﻿#include "widget.h"
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTranslator>
#include <QTimer>
#include <QDebug>
#include <QCamera>
#include <QCameraInfo>
#include <QTime>
#include <QThread>

#include <QtConcurrent>
#include <QProgressDialog>
 #include <functional>

#include "BCQmlRenderView.h"
#include "GenerateImage.h"
#include "BCDevice.h"

#include "NativeEvent.h"

#include "FormatConvert.h"


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    NativeEvent event;
    qApp->installNativeEventFilter(&event);

    qDebug() << "main thread" << QThread::currentThreadId() << (int)QThread::currentThreadId();

    QTranslator chinese(0);
    chinese.load("C:/Users/sl/Documents/media/lang_Chinses.qm",".");

    QTranslator english(0);
    english.load("C:/Users/sl/Documents/media/lang_English.qm",".");

    app.installTranslator(&chinese);

    QQmlApplicationEngine qmlEngine;


    qDebug() << "BCDevice thread" << BCDevice::getInstance()->currentThreadId();

    // 注册基础类型
    qmlRegisterType<BCQmlRenderView>("BCQmlRenderView", 1, 0, "BCQmlRenderView");
    qmlRegisterSingletonType<BCDevice>("BCDevice",1,0,"BCDevice",[](QQmlEngine *engine, QJSEngine *scriptEngine)->QObject*{
        Q_UNUSED(engine);
        Q_UNUSED(scriptEngine);

        return BCDevice::getInstance();
    });

    qmlEngine.rootContext()->setContextProperty("generate",BCDevice::getInstance()->getGenerate());
    qmlEngine.rootContext()->setContextProperty("event",&event);

        qmlEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (qmlEngine.rootObjects().isEmpty())
            return -1;

        QObject *root = qmlEngine.rootObjects().first();

        QObject::connect(root,SIGNAL(startInput(int)),BCDevice::getInstance(),SLOT(startMicrophone(int)));
        QObject::connect(root,SIGNAL(startOutput(int)),BCDevice::getInstance(),SLOT(startLoudspeaker(int)));
        QObject::connect(root,SIGNAL(startCamera(int)),BCDevice::getInstance(),SLOT(startCamera(int)));
        QObject::connect(root,SIGNAL(stopAll()),BCDevice::getInstance(),SLOT(stopAll()));

#if 0

    const int iterations = 20;

    // Prepare the vector.
    QVector<int> vector;
    for (int i = 0; i < iterations; ++i)
        vector.append(i);

    // Create a progress dialog.
    QProgressDialog dialog;
    dialog.show();
    dialog.setLabelText(QString("Progressing using %1 thread(s)...").arg(QThread::idealThreadCount()));

    // Create a QFutureWatcher and connect signals and slots.
    QFutureWatcher<void> futureWatcher;
    QObject::connect(&futureWatcher, &QFutureWatcher<void>::finished, &dialog, &QProgressDialog::reset);
    QObject::connect(&dialog, &QProgressDialog::canceled, &futureWatcher, &QFutureWatcher<void>::cancel);
    QObject::connect(&futureWatcher,  &QFutureWatcher<void>::progressRangeChanged, &dialog, &QProgressDialog::setRange);
    QObject::connect(&futureWatcher, &QFutureWatcher<void>::progressValueChanged,  &dialog, &QProgressDialog::setValue);

    // Our function to compute
    std::function<void(int&)> spin = [](int &iteration) {
        const int work = 1000 * 1000 * 40;
        volatile int v = 0;
        for (int j = 0; j < work; ++j)
            ++v;

        qDebug() << "iteration" << iteration << "in thread" << QThread::currentThreadId();
    };

    // Start the computation.
    futureWatcher.setFuture(QtConcurrent::map(vector, spin));

    // Display the dialog and start the event loop.
    dialog.exec();

    futureWatcher.waitForFinished();

    // Query the future to check if was canceled.
    qDebug() << "Canceled?" << futureWatcher.future().isCanceled();
#endif

    return app.exec();
}
